package mBio;

import java.io.*;
import java.util.*;
import rfBio.*;

public class psl3_to_gff{

	public static void main(String[] args)
		throws IOException, FileNotFoundException{
			
		if(args.length != 5){
			System.out.println("Wrong number of parameters!" + '\n' + "Usage: psl3_to_gff psl3_file gff_file min_score_to_include source feature.");
			System.exit(0);
		}

		// local variables
		Vector ref_vector = new Vector(100,100); 
		boolean addFlag = true;
		psl3 nov = new psl3();
		


		BufferedReader refFile = new BufferedReader ( new FileReader (args[0]));
		
		// read ref_file line by line, store in ref_vector
		while (true){
			String line = refFile.readLine();
			if (line == null)
			{
				break;
			}
			psl3 temp_psl3 = new psl3(line);
			//System.out.println(line);
			if( (temp_psl3.get_score() <= Type.toInt(args[2])) )
			{
				addFlag=false;
			}
			
			if(addFlag){
				for(int i =0; i < ref_vector.size(); i++){
					psl3 tmp = new psl3((psl3) ref_vector.get(i));
					if(temp_psl3.get_Q_name().equals(tmp.get_Q_name())){
						if( (temp_psl3.get_score() >= tmp.get_score())){			
							ref_vector.set(i, temp_psl3);
							addFlag=false;
						}
						else{
							addFlag=false;
						}	
					}
				}
			}

			if(addFlag){
				ref_vector.add(temp_psl3);
			}
			
			addFlag=true;
		} 		

		// close ref_ file
		refFile.close();
		
		//write output files
		
		PrintWriter o_file = new PrintWriter(new BufferedWriter(new FileWriter(args[1])));
				
		for(int i =0; i < ref_vector.size(); i++)
		{
			nov = (psl3) ref_vector.get(i);
			o_file.println( nov.toGff(args[3], args[4]));
		}
		
		o_file.close();

   }
}